<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>更新机构</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/css/oksub.css">
    <script type="text/javascript" src="/static/lib/loading/okLoading.js"></script>
    <script src="/static/lib/layui/layui.js"></script>
    <link type="text/css" rel="stylesheet" href="/static/css/metroStyle.css">
    <link type="text/css" rel="stylesheet" href="/static/css/treeSelectStyle.css">
    <script src="/static/js/jquery-1.11.2.min.js"></script>
    <script type="text/javascript" src="/static/js/jquery.ztree.all.js"></script>
    <script type="text/javascript" src="/static/js/treeSelect.2.0.js"></script>
    <script type="text/javascript" src="/static/js/xm-select.js"></script>
</head>
<body>
<div class="ok-body">
    <!--form表单-->
    <!--form表单-->
    <form class="layui-form layui-form-pane ok-form">
        <div class="layui-form-item">
            <label class="layui-form-label">部门代码<span style="color: red">*</span></label>
            <div class="layui-input-block">
                <input type="text" name="dept_code" id="dept_code" placeholder="请输入部门代码，且不重复！" autocomplete="off"
                       class="layui-input"
                       lay-verify="required">
                <input type="text" name="id" id="id" class="layui-input" style="display: none">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">部门名称<span style="color: red">*</span></label>
            <div class="layui-input-block">
                <input type="text" name="dept_name" id="dept_name" placeholder="请输入部门名称" autocomplete="off"
                       class="layui-input"
                       lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所属机构<span style="color: red">*</span></label>
            <div class="layui-input-block" name="dept_pname" id="dept_pname">

            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remark_describe" id="remark_describe" placeholder="请输入备注内容"
                          class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="edit">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary" id="close">关闭</button>
            </div>
        </div>
    </form>
</div>
</body>
<!--js逻辑-->

<script type="text/javascript">
    var data1;

    function child(d) {
        data1 = d;


        layui.use(["element", "form", "laydate", "okLayer", "okUtils"], function () {
            let $ = layui.$;
            let form = layui.form;
            let laydate = layui.laydate;
            let okLayer = layui.okLayer;
            let okUtils = layui.okUtils;
            okLoading.close();
            $('#id').val(data1.id);
            $('#dept_code').val(data1.dept_code);
            $('#dept_name').val(data1.dept_name);
            $('#tree_dept').val(data1.tree_dept);
            $('#dept_pname').val(data1.dept_pname);
            $('#remark_describe').val(data1.remark_describe);
            form.render();
            // 回显上级单位
            var initvalue;
            if(data1.tree_dept===null){
                initvalue=0;
            }else{
                initvalue=data1.tree_dept
            }
            var demo3 = xmSelect.render({
                el: '#dept_pname',
                model: {label: {type: 'text'}},
                radio: true,
                clickClose: true,
                initValue:[initvalue],
                prop:{
	                name:'dept_name',
                    value:'id',
                },
                tree: {
                    show: true,
                    strict: false,
                    expandedKeys: [-1],
                },
                height: 'auto',
                data() {
                    return []
                }
            });
            var select=false;
            $.ajax({
                url: "{% url 'config:depts_list1' %}",
                type: "GET",
                dataType:"json",
                success: function (ret) {
                    var node = ret;
                    demo3.update({
                        data: node.data,
                        autoRow: true,
                    })
                },
                error: function () {
                }
            });
            // 提交检查
            form.on("submit(edit)", function (data) {
                if (data.field.select == data1.id) {
                okLayer.redCrossMsg('不能选择自己为上级单位');
                return false;
                }
                okUtils.ajax("/config/depts_edit/" + data.field.id, "post", data.field, true).done(function (response) {
                    okLayer.greenTickMsg(response.msg, function () {
                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                        window.parent.location.reload();
                    });
                }).fail(function (error) {
                    console.log(error)
                });
                return false;
            });
            $("#close").click(function () {
                let index = parent.layer.getFrameIndex(window.name); //获取当前窗口的name
                window.parent.location.reload();
                parent.layer.close(index);

            });
        })
    };


</script>
</html>
